define(["angularAMD"], function (angularAMD) {
    angularAMD.service('A0402Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$css',
        function($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $css){
            var service = this;
            service.genController = function(options, controller){
                controller.init = function(){
                    controller.plot_info_details = [];
                    controller.plot_info = [];
                };
                controller.loadPlotList = function(){
                    $MMWService.showQueryListPNIG({
                        page_mode: 3,
                        pageRecord:50,
                        condition: [
                            {name:$filter('translate')('common.queryList.plot_no'), column_name:'LotNo', query_mode:'2', value:controller.plot_no}
                        ],
                        header: [
                            {label:$filter('translate')('common.queryList.plot_no'), code:'page_no'}
                        ],
                        selectCode:controller.plot_no,
                        setCondition:function(conditions){
                            conditions.push({column_name:'status', query_mode:'0', value:'0', merge_condition_model:'and'});
                            conditions.push({column_name:'status', query_mode:'0', value:'1', merge_condition_model:'and'});
                            conditions.push({column_name:'status', query_mode:'0', value:'2', merge_condition_model:'and'});
                            return conditions;
                        },
                        confirm : function(selectItem){
                            controller.plot_no = selectItem.page_no;
                            //controller.search();
                        }
                    });
                };
                /*
                controller.loadMoList = function(){
                    $MMWService.showQueryListPNIG({
                        page_mode: 1,
                        condition: [
                            {name:$filter('translate')('common.queryList.wo_c'), column_name:'MONo', query_mode:'2', value:''}
                        ],
                        header: [
                            {label:$filter('translate')('common.queryList.wo_c'), code:'page_no'}
                        ],
                        selectCode:controller.mo_no,
                        setCondition:function(conditions){
                            conditions.push({column_name:'MOSTATE', query_mode:'0', value:'3', merge_condition_model:'AND' });
                            return conditions;
                        },
                        confirm : function(selectItem){
                            controller.mo_no = selectItem.page_no;
                            //controller.search();
                        }
                    });
                };
                */
                controller.search = function(){
                    if(controller.plot_no && controller.plot_no!=''){
                        var content = {
                            module:'0',
                            plot_no:controller.plot_no
                        };
                        subandmerge_plot_info_get(content, function(data){
                            controller.plot_info_details = data.plot_info;
                            controller.plotItem = undefined;
                            // controller.wo_no = '';
                            controller.plot_info = [];
                            //20180105 modify by Dustdusk for 當只有一筆的時候則自動選擇
                            if(controller.plot_info_details && controller.plot_info_details.length == 1){
                                controller.item_click(0, controller.plot_info_details[0]);
                            }
                        });
                    } else {
                        $rootScope.showAlert($filter('translate')('A0402.msg.plot_no_empty'));
                    }
                };
                controller.item_click = function(index, item){
                    if(!controller.isSelect(item)){
                        controller.plotItem = item;
                        controller.keep_plot_no = '0';
                        controller.sub_plot_method = '0';
                        controller.sub_plot_qty = 0;
                        controller.plot_info = [];
                        controller.searchWoNo();
                    }
                };
                controller.isSelect = function(item){
                    if(!controller.plotItem){
                        return false;
                    } else {
                        return (controller.plotItem.plot_no == item.plot_no)
                        && (controller.plotItem.op_no == item.op_no)
                        && (controller.plotItem.status == item.status);
                    }
                };
                controller.searchWoNo = function(){
                    if(controller.plotItem && controller.plotItem.plot_no!=''){
                        // if(controller.plotItem.wo_no && controller.plotItem.wo_no!=''){
                            var content = {
                                module:'1',
                                wo_no:controller.plotItem.wo_no,
                                plot_no:controller.plotItem.plot_no,
                                op_no:controller.plotItem.op_no,
                                status:controller.plotItem.status
                            };
                            subandmerge_plot_info_get(content, function(data){
                                if(data && data !== null)
                                    controller.plot_info = data.plot_info;
                            });
                        // } else {
                        //     $rootScope.showAlert($filter('translate')('A0402.msg.wo_no_empty'));
                        // }
                    } else {
                        $rootScope.showAlert($filter('translate')('A0402.msg.plot_item_empty'));
                    }
                };
                controller.confirm = function(callback){
                    if(controller.plotItem && controller.plot_info.length > 0){
                        var temp = $filter('filter')(controller.plot_info,{isSelect:true});
                        if(temp && temp.length > 0){
                            var content = {
                                plot_no: controller.plotItem.plot_no,
                                plot_stamp: controller.plotItem.plot_stamp,
                                op_no: controller.plotItem.op_no,
                                status: controller.plotItem.status,
                                qty: controller.plotItem.qty,
                                plot_info: temp,
                                userno: config.cache.account
                            };
                            merge_plot_lot_process(content, function(){
                                $rootScope.showToast($filter('translate')('A0402.msg.success'));
                                controller.plot_info_details = [];
                                controller.plotItem = undefined;
                                // controller.wo_no = '';
                                controller.plot_info = [];
                                if(callback){
                                    callback();
                                } else {
                                    controller.search();
                                }
                            });
                        } else {
                            $rootScope.showAlert($filter('translate')('A0402.msg.plot_item_empty'));
                        }

                    } else {
                        $rootScope.showAlert($filter('translate')('A0402.msg.plot_item_empty'));
                    }
                };
                return controller;
            };

            service.init = function(options){
                var content = {
                    module:'0',
                    plot_no:options.plotItem.plot_no,
                };
                subandmerge_plot_info_get(content, function(data){
                    $mdDialog.dialog(
                        'module/moduleA/program/A04/A0402/A0402.tmp.html',
                        function(dialog){
                            var controller = service.genController(options, dialog);
                            controller.beforeShown = function(){
                                $css.add('module/moduleA/program/A04/A0402/A0402.css');
                                dialog.init();
                                //20190709 modify by Dustdusk for M#59757: 帶回資料可能多筆，需要過濾
                                dialog.plot_info_details = $filter('filter')(data.plot_info, {plot_no:content.plot_no}, true);
                                dialog.plotItem = dialog.plot_info_details[0];
                                dialog.plot_no = options.plotItem.plot_no;
                                dialog.searchWoNo();
                            };
                            controller.back = function () {
                                dialog.hide();
                                $css.remove('module/moduleA/program/A04/A0402/A0402.css');
                            };
                            controller.dialog_confirm = function () {
                                dialog.confirm(function(){
                                    dialog.back();
                                    if(options.feedback)
                                        options.feedback();
                                });
                            };
                            return controller;
                        }
                    );
                });
            };
            function subandmerge_plot_info_get(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_WIP.subandmerge_plot_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            }

            function merge_plot_lot_process(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_WIP.merge_plot_lot_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            }
        }]);
});
